package com.jingdianjichi.subject.domain.service.impl;


import com.alibaba.fastjson.JSON;
import com.jingdianjichi.api.subject.entity.dto.PracticeSubjectDTO;
import com.jingdianjichi.api.subject.entity.po.SubjectPO;
import com.jingdianjichi.api.subject.entity.vo.PracticeSubjectDetailVO;
import com.jingdianjichi.subject.common.enums.CategoryTypeEnum;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.comvert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;

    /**
     * 新增题目标签
     *
     * @param subjectLabelBO
     */
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));

        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;

    }

    /**
     * 更新题目标签
     *
     * @return
     */
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));

        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 删除题目标签
     *
     * @return
     */
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.delete.bo:{}", JSON.toJSONString(subjectLabelBO));

        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);

        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }


    /**
     * 查询分类下题目标签
     *
     * @return
     */
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果当前分类是一级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if (CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()) {
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectCategory.getId());
            List<SubjectLabel> subjectLabelList = subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> labelBOList = SubjectLabelConverter.INSTANCE.convertToLabelBO(subjectLabelList);
            return labelBOList;
        }
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(subjectLabelBO.getCategoryId());
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        //通过分类id查询题目id
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        //通过题目id查询标签信息
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        LinkedList<SubjectLabelBO> boList = new LinkedList<>();
        for (SubjectLabel subjectLabel : labelList) {
            SubjectLabelBO labelBO = new SubjectLabelBO();
            labelBO.setId(subjectLabel.getId());
            labelBO.setLabelName(subjectLabel.getLabelName());
            labelBO.setCategoryId(subjectLabelBO.getCategoryId());
            labelBO.setSortNum(subjectLabel.getSortNum());
            boList.add(labelBO);
        }
        return boList;
    }
    /**
     * 获取套卷题目信息
     */
    @Override
    public List<PracticeSubjectDetailVO> getPracticeList(PracticeSubjectDTO practiceSubjectDTO) {
        List<PracticeSubjectDetailVO> practiceSubjectListVOS = new LinkedList<>();
        //避免重复
        List<Long> excludeSubjectIds = new LinkedList<>();

        //设置题目数量，之后优化到nacos动态配置
        Integer radioSubjectCount = 10;
        Integer multipleSubjectCount = 6;
        Integer judgeSubjectCount = 4;
        Integer totalSubjectCount = 20;

        //查询单选
        practiceSubjectDTO.setSubjectCount(radioSubjectCount);
        practiceSubjectDTO.setSubjectType(SubjectInfoTypeEnum.RADIO.getCode());
        assembleList(practiceSubjectDTO, practiceSubjectListVOS, excludeSubjectIds);
        //查询多选
        practiceSubjectDTO.setSubjectCount(multipleSubjectCount);
        practiceSubjectDTO.setSubjectType(SubjectInfoTypeEnum.MULTIPLE.getCode());
        assembleList(practiceSubjectDTO, practiceSubjectListVOS, excludeSubjectIds);
        //查询判断
        practiceSubjectDTO.setSubjectCount(judgeSubjectCount);
        practiceSubjectDTO.setSubjectType(SubjectInfoTypeEnum.JUDGE.getCode());
        assembleList(practiceSubjectDTO, practiceSubjectListVOS, excludeSubjectIds);
        //补充题目
        if (practiceSubjectListVOS.size() == totalSubjectCount) {
            return practiceSubjectListVOS;
        }
        Integer remainCount = totalSubjectCount - practiceSubjectListVOS.size();
        practiceSubjectDTO.setSubjectCount(remainCount);
        practiceSubjectDTO.setSubjectType(1);
        assembleList(practiceSubjectDTO, practiceSubjectListVOS, excludeSubjectIds);
        return practiceSubjectListVOS;
    }

    private List<PracticeSubjectDetailVO> assembleList(PracticeSubjectDTO practiceSubjectDTO, List<PracticeSubjectDetailVO> list, List<Long> excludeSubjectIds) {
        practiceSubjectDTO.setExcludeSubjectIds(excludeSubjectIds);
        List<SubjectPO> subjectPOList = subjectLabelService.getPracticeSubject(practiceSubjectDTO);
        if (CollectionUtils.isEmpty(subjectPOList)) {
            return list;
        }
        subjectPOList.forEach(subjectPO -> {
            PracticeSubjectDetailVO vo = new PracticeSubjectDetailVO();
            vo.setSubjectId(subjectPO.getId());
            vo.setSubjectType(subjectPO.getSubjectType());
            excludeSubjectIds.add(subjectPO.getId());
            list.add(vo);
        });
        return list;
    }
}
